package com.dinner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dinner.auto.JSONAuthentication;
import com.dinner.entity.AppraiseMenu;

import com.dinner.mapper.AppraiseMenuMapper;
import com.dinner.mapper.DiUserMapper;
import com.dinner.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinner
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/appraise-menu")
public class AppraiseMenuController extends JSONAuthentication {
    @Autowired
    AppraiseMenuMapper appraiseMenuMapper;
    @Autowired
    DiUserMapper diUserMapper;
    @ApiOperation("添加菜品评价")
    @PostMapping("/add")
    public void addOrderMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("menuId") int menuId, @RequestParam("userId") int userId, @RequestParam("appraise")int appraise,@RequestParam("content") String content) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("menu_id",menuId);
        wrapper.eq("user_id",menuId);
        List<AppraiseMenu> list=appraiseMenuMapper.selectList(wrapper);
        if(list.size()>0){
            Result error = Result.error().message("以评价，请勿重复评价");
            this.WriteJSON(request, response, error);
        }else{
            AppraiseMenu appraiseMenu=new AppraiseMenu();
            appraiseMenu.setMenuId(menuId);
            appraiseMenu.setUserId(userId);
            appraiseMenu.setAppraise(appraise);
            appraiseMenu.setContent(content);
            if(appraiseMenuMapper.insert(appraiseMenu)>0){
                Result ok=Result.ok();
                this.WriteJSON(request, response, ok);
            }
            Result error = Result.error().message("添加失败");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看菜品的评分通过菜品id")
    @PostMapping("/lookMenuMenuByid1")
    public void lookMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("menuId") int menuId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("menu_id",menuId);
        List<AppraiseMenu> list=appraiseMenuMapper.selectList(wrapper);
        int score=0;
        for(AppraiseMenu a:list){
            score+=a.getAppraise();
        }
        float average=score/list.size();
        if(list.size()>0){
            Result ok = Result.ok().data("average",average);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("该菜品还没有评价，快去评价一下吧！");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看菜品评价通过菜品id并返回评价用户")
    @PostMapping("/lookMenuMenuByid")
    public void lookMenuContent(HttpServletRequest request, HttpServletResponse response, @RequestParam("menuId") int menuId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("menu_id",menuId);
        List<AppraiseMenu> list=appraiseMenuMapper.selectList(wrapper);
        for(AppraiseMenu a:list){
            a.setDiUser(diUserMapper.selectById(a.getUserId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("list",list);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("该菜品还没有评价，快去评价一下吧！");
            this.WriteJSON(request, response, error);
        }
    }
}

